/*
 * @lc app=leetcode.cn id=24 lang=typescript
 *
 * [24] 两两交换链表中的节点
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function swapPairs(head: ListNode | null): ListNode | null {
    if (!head) {
        return null;
    }
    if (!head.next) {
        return head;
    }
    let nextTwo: ListNode | null = null;
    const next = head.next;
    nextTwo = next.next;
    next.next = head;
    head.next = swapPairs(nextTwo);
    return next
};
// @lc code=end

